local shenglun = fk.CreateSkill{
  name = "iberia__shenglun",
}

Fk:loadTranslationTable{
  [shenglun.name] = "胜论",
  [":"..shenglun.name] = "当你受到伤害后，你可以尽可能明置一名角色的所有手牌。每因此明置一种花色，其摸一张牌。",

  ["#iberia__shenglun-invoke"] = "胜论：可以尽可能明置一名角色的所有手牌。每因此明置一种花色，其摸一张牌"
}

local I = require "packages/iberia/utility"

shenglun:addEffect(fk.Damaged, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(shenglun.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(false), function (p)
      return table.find(p:getCardIds("h"), function (id)
        return not S.isShown(id)
      end)
    end)

    if #targets == 0 then return end
    local to = room:askToChoosePlayers(player, {
      targets = targets,
      max_num = 1,
      min_num = 1,
      skill_name = shenglun.name,
      cancelable = true,
      prompt = "#iberia__shenglun-invoke",
    })
    
    if #to == 0 then return end
    event:setCostData(self, to[1])
    return true
  end,
  on_use = function (self, event, target, player, data)
    local to = event:getCostData(self) ---@type ServerPlayer
    local hide = table.filter(to:getCardIds("h"), function (id)
      return not S.isShown(id)
    end)

    S.ShowCards(to, hide, Player.HistoryGame)
    local suits = {}
    for i = 1, #hide do
      if to.dead then break end
      local card = Fk:getCardById(hide[i])
      suits[card.suit] = (suits[card.suit] or 0) + 1
      if suits[card.suit] == 1 then
        to:drawCards(1, shenglun.name)
      end
    end
  end
})

return shenglun